package com.zany.net.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.zany.net.dto.Header;
import com.zany.net.dto.InterfaceSerializer;
import com.zany.net.dto.request.QueryMoneyReq;
import com.zany.net.dto.request.RequestBody;
import com.zany.net.dto.request.RequestEntity;
import com.zany.net.dto.response.QueryMoneyRes;
import com.zany.net.dto.response.ResponseEntity;

/**
 * 预缴费查询接口
 */
public class QueryMoneyServiceImpl extends AbsConvertJson implements NetService {

    private RequestEntity requestEntity;


    @Override
    public ResponseEntity handler(String jsonStr) {
        convertRequestEntity(jsonStr);
        RequestBody body = requestEntity.getBody();
        QueryMoneyReq queryMoneyReq = (QueryMoneyReq) body;
        return null;
    }

    @Override
    public ResponseEntity handler(RequestEntity requestEntity) {
        QueryMoneyReq body = (QueryMoneyReq) requestEntity.getBody();
        //进行数据库操作.
        QueryMoneyRes queryMoneyRes = new QueryMoneyRes();
        queryMoneyRes.setAge("10");
        queryMoneyRes.setClassName("一年二班");
        queryMoneyRes.setIdentityCode("20040319900964545");
        queryMoneyRes.setMoney("100");
        queryMoneyRes.setOwedId("1");
        queryMoneyRes.setSex("男");
        queryMoneyRes.setStuId("1");
        queryMoneyRes.setStuName("张海");
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setBody(queryMoneyRes);
        responseEntity.setHeader(requestEntity.getHeader());
        return responseEntity;
    }

    @Override
    public RequestEntity convert(JsonObject jsonObject) {
//        JsonElement bodyElement = jsonObject.get(BODY);
//        JsonElement headerElement = jsonObject.get(HEADER);
//        JsonObject asJsonObject = headerElement.getAsJsonObject();
//        String msgcd = asJsonObject.get(MSGCD).getAsString();
//        String snddt = asJsonObject.get("snddt").getAsString();
//        String sndappcd = asJsonObject.get("sndappcd").getAsString();
//        String seqnb = asJsonObject.get("seqnb").getAsString();
//        JsonObject asJsonBodyObject = bodyElement.getAsJsonObject();
//        RequestEntity requestEntity = new RequestEntity();
//        Header header = new Header();
//        header.setMsgcd(msgcd);
//        header.setSnddt(snddt);
//        header.setSndappcd(sndappcd);
//        header.setSeqnb(seqnb);
//        QueryMoneyReq requestBody = new QueryMoneyReq();
//        String org_code = asJsonBodyObject.get("org_code").getAsString();
//        String query_param = asJsonBodyObject.getAsJsonObject("query_param").getAsString();
//        String query_type = asJsonBodyObject.getAsJsonObject("query_type").getAsString();
//        requestBody.setOrgCode(org_code);
//        requestBody.setQueryParam(query_param);
//        requestBody.setQueryType(query_type);
//        requestEntity.setHeader(header);
//        requestEntity.setBody(requestBody);
//        return requestEntity;
        InterfaceSerializer<QueryMoneyReq> interfaceSerializer = InterfaceSerializer.interfaceSerializer(getReqEntity());
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(RequestBody.class, interfaceSerializer)
                .create();
        RequestEntity entity = gson.fromJson(jsonObject.toString(), RequestEntity.class);
        QueryMoneyReq body = (QueryMoneyReq) entity.getBody();
        Header header = entity.getHeader();
        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setBody(body);
        requestEntity.setHeader(header);
        return requestEntity;
    }

    @Override
    void setupEntity(RequestEntity requestEntity) {
        this.requestEntity = requestEntity;
    }

    @Override
    Class getReqEntity() {
        return QueryMoneyReq.class;
    }
}
